import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable } from "rxjs";
import { CookieService } from "ngx-cookie-service";

@Injectable({
  providedIn: "root",
})
export class UserAuthService {
  userInfo = {
    token: this.cookieService.get("token"),
    userName: localStorage.getItem("userName"),
    role: localStorage.getItem("ROLE"),
  }

  constructor(private http: HttpClient, private cookieService: CookieService) { }

  // 获取验证码
  getCaptcha(): Observable<any> {
    return this.http.get("v1/user/captcha")
  }

  //获取手机验证码
  getPhoneCaptcha(phoneNum, isReg): Observable<any> {
    return this.http.post("v1/user/verifyphonenum", { phoneNum, isReg })
  }

  //用户注册
  userRegister(
    userName: string,
    password: string,
    captcha: string,
    phoneNumber?: string,
    phoneVerificationCode?: String
  ): Observable<any> {
    return this.http.post("v1/user/registration", {
      userName,
      password,
      captcha,
      phoneNumber,
      phoneVerificationCode
    })
  }

  getCurrentUser(obj: any) {
    this.userInfo.userName = obj && obj.userName;
    this.userInfo.role = obj && obj.role;
  }

  // 用户登录
  userLogIn(userName: string, password: string): Observable<any> {
    return this.http.post("v1/user/login", { userName, password })
  }

  // 重名检测
  userExistTest(userName: string): Observable<any> {
    return this.http.get(`v1/user/checkusername?username=${userName}`)
  }

  getCsrfToken(): Observable<any> {
    return this.http.options("v1/token")
  }

  userLogOut(): Observable<any> {
    return this.http.get("v1/user/logout")
  }

  //用户信息展示
  getUserMessage(): Observable<any> {
    return this.http.get("v1/user/profile")
  }

  //用户注销
  accountLogOff(password: string): Observable<any> {
    return this.http.post("/v1/user/unsubscribe", { password })
  }

  //修改密码
  passwordModify(oldPass: string, newPass: string): Observable<any> {
    return this.http.post("/v1/user/changepass", { oldPass, newPass })
  }

  //查询是否实名认证
  getUserVerification(userid: number): Observable<any> {
    return this.http.get("/v1/user/checkauthentication?userid=" + userid)
  }

  //实名认证
  userVerification(userid: number, verificationName: string, verificationID: string): Observable<any> {
    return this.http.post("/v1/user/authentication", { userid, verificationName, verificationID })
  }

  //手机登录
  telLogin(phoneNum: string, verificationCode: string): Observable<any> {
    return this.http.post('/v1/user/loginbyphone', { phoneNum, verificationCode })
  }

  //查询是否绑定手机
  checkTele(userId: number): Observable<any> {
    return this.http.get(`/v1/user/checkuserphoneused?userId=${userId}`)
  }

  //绑定手机
  teleConnect(userId: number, phoneNumber: string, phoneVerificationCode: String): Observable<any> {
    return this.http.post('/v1/user/updatephonenum', {
      userId, phoneNumber, phoneVerificationCode
    })
  }

  //上传头像
  uploadAvatar(formData: FormData): Observable<any> {
    return this.http.post('v1/user/changeProfilePhoto', formData)
  }

  //获取头像
  getAvatar(userId): Observable<any> {
    return this.http.get(`v1/user/getProfilePhoto?userId=${userId}`)
  }
}
